﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace Spore.Utils
{
    public class ClientLister : ChannelBase<Socket>
    {
        public ClientLister(Socket socket)
            : base(socket)
        {
 
        }

        protected override void OnOpen()
        {
            this.socket.Listen(1000);
            if (!this.socket.AcceptAsync(this.readAsy))
            {
                this.OnReadComplete();
            }
        }

        protected override void OnReadData()
        {
        //    if (this.readAsy.AcceptSocket == null)
        //    {
              
        //        return;
        //    }
            if (this.readAsy.AcceptSocket.RemoteEndPoint != null)
            {
                Socket socket = readAsy.AcceptSocket;
                readAsy.AcceptSocket = null;
                this.BufferReader.AddElement(socket);
                this.OnReadElement();
                if (!this.socket.AcceptAsync(readAsy))
                {
                    this.OnReadComplete();
                }
              
            }
            
        }

        protected override void OnWriteData()
        {

        }
    }
}
